#include"queue.h"

void queueinit(Queue* pq)
{
	pq->size = 0;
	pq->phead = pq->tail = NULL;

}
void queuedestroy(Queue* pq)
{
	assert(pq);
	Qnode* cur = pq->phead;
	while (cur)
	{
		Qnode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->phead = pq->tail = NULL;
	pq->size = 0;
}
void queuepush(Queue* pq, typedata x)
{
	assert(pq);
	Qnode* newnode = (Qnode*)malloc(sizeof(Qnode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}
	newnode->data = x;
	newnode->next = NULL;
	if (pq->tail == NULL)
	{
		assert(pq->phead == NULL);
		pq->phead = newnode;
		pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;

	}
	pq->size++;
}
void queuepop(Queue* pq)
{
	assert(pq);
	if (pq->phead->next == NULL)
	{
		free(pq->phead);
		pq->phead = pq->tail = NULL;
	}
	else
	{
		Qnode* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}
	pq->size--;
}

bool queueempty(Queue* pq)
{
	assert(pq);
	return pq->size == 0;
}
typedata queuefront(Queue* pq)
{
	assert(pq);
	assert(!queueempty(pq));
	return pq->phead->data;

}
typedata queueback(Queue* pq)
{
	assert(pq);
	assert(!queueempty(pq));

	return pq->tail->data;
}
typedata queuesize(Queue* pq)
{
	assert(pq);
	assert(!queueempty(pq));
	return pq->size;
}